﻿using Microsoft.Extensions.Options;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;

namespace Ln.Fram.Caching.FreeRedis;

[Dependency(ReplaceServices = true)]
public class LnDistributedCacheKeyNormalizer : IDistributedCacheKeyNormalizer, ITransientDependency
{
	protected ICurrentTenant CurrentTenant { get; }

	protected AbpDistributedCacheOptions DistributedCacheOptions { get; }

	public LnDistributedCacheKeyNormalizer(
			ICurrentTenant currentTenant,
			IOptions<AbpDistributedCacheOptions> distributedCacheOptions)
	{
		CurrentTenant = currentTenant;
		DistributedCacheOptions = distributedCacheOptions.Value;
	}

	public virtual string NormalizeKey(DistributedCacheKeyNormalizeArgs args)
	{
		var normalizedKey = $"{DistributedCacheOptions.KeyPrefix}{args.Key}";

		//if (!args.IgnoreMultiTenancy && CurrentTenant.Id.HasValue)
		//{
		//    normalizedKey = $"t:{CurrentTenant.Id.Value},{normalizedKey}";
		//}

		return normalizedKey;
	}
}
